//Locale代理类
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

//Locale资源类
class DemoLocalizations {
  static const String CURRENT_LANGUAGE = "current_language";
  final Locale locale;

  DemoLocalizations(this.locale);

  static const String ZH_HANT_TW = "zh_Hant_TW";
  static const String ZH_HANS_CN = "zh_Hans_CN";
  static const String EN_US = "en_US";

  static String sCurrentLanguage;

  static Map<String, Map<String, String>> _localizedValues = {
    'zh_Hant_TW': {
      'task title': 'Flutter 實例',
      'titlebar title': 'Flutter 實例主頁面',
      'click tip': 'You have pushed the button this many times:',
      'inc': 'Increment'
    },
    'zh_Hans_CN': {
      'task title': 'Flutter 示例',
      'titlebar title': 'Flutter 示例主页面',
      'click tip': '你一共点击了这么多次按钮：',
      'inc': '增加'
    },
    'en_US': {
      'task title': 'Flutter Demo',
      'titlebar title': 'Flutter Demo Home Page',
      'click tip': 'You have pushed the button this many times:',
      'inc': 'Increment'
    },
  };

  get taskTitle {
    return _localizedValues[sCurrentLanguage]['task title'];
  }

  get titleBarTitle {
    return _localizedValues[locale.toString()]['titlebar title'];
  }

  get clickTop {
    return _localizedValues[locale.toString()]['click tip'];
  }

  get inc {
    return _localizedValues[locale.toString()]['inc'];
  }

  static DemoLocalizations of(BuildContext context) {
    return Localizations.of(context, DemoLocalizations);
  }
}

class DemoLocalizationsDelegate extends LocalizationsDelegate<DemoLocalizations> {
  const DemoLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) {
    switch (locale.toString()) {
      case DemoLocalizations.ZH_HANT_TW:
        DemoLocalizations.sCurrentLanguage = 'zh_Hant_TW';
        break;
      case DemoLocalizations.ZH_HANS_CN:
        DemoLocalizations.sCurrentLanguage = 'zh_Hans_CN';
        break;
      case DemoLocalizations.EN_US:
        DemoLocalizations.sCurrentLanguage = 'en_US';
        break;
    }
    return true;
  }

  @override
  Future<DemoLocalizations> load(Locale locale) {
    print("load locale == " + locale.toString());
    return new SynchronousFuture<DemoLocalizations>(new DemoLocalizations(locale));
  }

  @override
  bool shouldReload(LocalizationsDelegate<DemoLocalizations> old) {
    return false;
  }

  static DemoLocalizationsDelegate delegate = const DemoLocalizationsDelegate();
}
